//
//  ListenListVC.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/25.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ListenListVC: TTBaseVC {
    
    var id:Int = 0
    var Indext:Int = 999999
    
    var titleStr:String = ""
    let ListenTableViewCellID = "ListenTableViewCell"
    
    var listModelArr:[ListenListModel] = [ListenListModel]()
    
    lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.bounces = true
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(ListenTableViewCellID), forCellReuseIdentifier: ListenTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()
    
    var redView: TimeRedPacketView?
    
    lazy var headView: UIView = {
        let view = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_SCALE_355X(x: 140) + 20))
        view.backgroundColor = WhiteBackColor
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
        setData()
        setBannerView()
        self.navigationItem.title = titleStr
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.redView = TimeRedPacketView.init(frame: FRAME_CGFloat(SCREEN_WIDTH - 80, 300, 80, 80), viewName: "栏目列表页")
        self.view.addSubview(self.redView!)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.redView?.removeFromSuperview()
    }
    
    func setUI() {
        self.view.addSubview(self.tableView)
        self.tableView.tableHeaderView = self.headView
    }
    
    func setData() {
        MTProgressHUD.show()
        HomeRequestModel.loadProgram(categoryId:[id],parameters: ["startPoint":1,"maxPage":999]) { (success, message, model, page) in
            if success && model != nil {
                self.listModelArr = model!
                self.tableView.reloadData()
            } else {
                TTAlertView.showMessage(message: message)
            }
            if self.listModelArr.count == 0 {
                self.tableView.setNoData(nodata: true)
            } else {
                self.tableView.setNoData(nodata: false)
            }
            MTProgressHUD.hidded(true)
        }
    }
    
    func setBannerView() {
        HomeRequestModel.loadCateBannerList(parameters: ["categoryId":id, "type":1]) { (success, message, model) in
            if success && model != nil {
                let topBanner = ListenListTopBannerView.init(frame: FRAME_CGFloat(10, 10, SCREEN_WIDTH - 20, SCREEN_SCALE_355X(x: 140)))
                topBanner.layer.cornerRadius = 11
                topBanner.layer.masksToBounds = true
                topBanner.delegate = self
                topBanner.bannerList = model
                self.headView.addSubview(topBanner)
            } else {
                self.tableView.tableHeaderView = UIView.init(frame: .zero)
            }
        }
    }
}

// MARK: *******  协议  ********
extension ListenListVC: UITableViewDelegate,UITableViewDataSource, ListenTableViewCellDelegate, ListenListTopBannerViewDelegate {
    func ListenListTopBannerViewClick(cSrc: String, isH5: Int) {
        if isH5 == 1 {
            if cSrc != "0" {
                let playVc = PlayVC()
                playVc.id = Int(cSrc)!
                ISLOGIN_PUSHVIEWCONTROLLER(playVc)
            }
        } else {
            let vc = WebVc()
            vc.urlString = cSrc
            vc.modalTransitionStyle = .crossDissolve
            self.present(vc, animated: true, completion: nil)
        }
    }
    
    func playActionDele(cell: UITableViewCell, paly: Bool) {
        let index:IndexPath = self.tableView.indexPath(for: cell) as! IndexPath
        if paly {
            self.Indext = index.row
        } else {
            self.Indext = 99999
        }
        self.tableView.reloadData()
    }
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listModelArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 95.5 + SCREEN_SCALE_355X(x: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ListenTableViewCellID) as! ListenTableViewCell
        if indexPath.row == self.Indext {
            cell.onPlay = true
        } else {
            cell.onPlay = false
        }
        cell.modelList = self.listModelArr[indexPath.row]
        cell.delegate = self
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.listModelArr[indexPath.row].useH5 == 1 {
            let playVc = PlayVC()
            playVc.id = self.listModelArr[indexPath.row].id
            ISLOGIN_PUSHVIEWCONTROLLER(playVc)
        } else {
            
        }
    }
    
}
